<?php
class Model_Vm extends Core_Database
{
	/**
	 * 
	 * kiem tra ten may ao da ton tai
	 * @param string $vmname
	 * @return bool true: co ton tai false: khong ton tai
	 */
  	public function checkVmname($vmname)
    {
        $sql = 'select vm_id from vms where vmname = ?';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(1, $vmname,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetchAll();
        if(count($result) == 0)
        	return false;
       return true; 	
    }
    
    /**
     * 
     * them may ao
     * @param unknown_type $user_id
     * @param unknown_type $package_id
     * @param unknown_type $vmname
     * @param unknown_type $duration
     */
    public function insertVm($user_id,$package_id,$vmname,$duration)
    {
    	$sql = 'insert into vms(user_id,package_id,vmname,duration,created_date,start_date) values (:user_id,:package_id,:vmname,:duration,NOW(),NOW())';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    	$sth->bindParam(':package_id', $package_id, PDO::PARAM_INT);
    	$sth->bindParam(':vmname', $vmname, PDO::PARAM_STR);
    	$sth->bindParam(':duration', $duration, PDO::PARAM_INT);
    	return $sth->execute();
    }
    
 	/**
    * Lay thong tin cua mot may ao
    * 
    * @param : vm_id
    * @return : array : neu ton tai  null : neu khong ton tai
    * 
    * */
    public function getVm($vm_id)
    {
     $sql = 'select vms.*,package.*,username from vms,package,users where vm_id = :vm_id and vms.package_id = package.package_id  and vms.user_id = users.user_id';
     $sth = $this->_db->prepare($sql);
     $sth->bindParam(':vm_id',$vm_id,PDO::PARAM_INT);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
    }
    
    /**
     * 
     * Update thong tin may ao sau khi startVm
     * @param int $vm_id
     * @param int $duration
     * @param int $extratime
     * @param string $ip
     * @param string $domainname
     * @param string $default_name
     * @return bool true: thanh cong false : that bai
     */
    public function updateStartVm($vm_id,$duration,$extratime,$ip,$domainname,$default_name)
    {
    	$sql = 'update vms set status = 1, duration = :duration + :extratime, extratime = 0,start_date = NOW(), ip = :ip, domainname = :domainname, default_name = :default_name where vm_id = :vm_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':vm_id',$vm_id,PDO::PARAM_INT);
  	    $sth->bindParam(':duration', $duration, PDO::PARAM_INT);
  	    $sth->bindParam(':extratime',$extratime,PDO::PARAM_INT);
  	    $sth->bindParam(':ip',$ip,PDO::PARAM_STR);
  	    $sth->bindParam(':domainname',$domainname,PDO::PARAM_STR);
  	    $sth->bindParam(':default_name',$default_name,PDO::PARAM_STR);
    	return $sth->execute();
    }
    
    /**
     * 
     * Update thong tin may ao sau khi stop
     * @param int $vm_id
     * @param int $usedtime
     * @return bool true: thanh cong false : that bai
     */
    public function updateStopVm($vm_id,$usedtime)
    {
    	$sql = 'update vms set status = 0, usedtime = :usedtime where vm_id = :vm_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':vm_id',$vm_id,PDO::PARAM_INT);
  	    $sth->bindParam(':usedtime', $usedtime, PDO::PARAM_INT);
    	return $sth->execute();
    }

    /**
     * Lay thoi gian hien tai
     * @return datetime
     */
    public function getNow()
    {
    	$sql = 'select now()';
    	$sth = $this->_db->prepare($sql);
    	$sth->execute();
    	$result = $sth->fetchAll();
    	return $result[0][0];
    }
    
	/**
    * Lay thong tin cua cac may ao cua mot nguoi dung
    * 
    * @param : int user_id = 0 tat ca 
    * @param : status :0 dang dung ; 1: dang chay ; 2 : all
    * @return : array : neu ton tai  null : neu khong ton tai
    * 
    * */
    public function getVms($user_id = 0,$status = 2)
    {
     $sqlUser = ' '; 	
     if($user_id > 0)
     		$sqlUser = ' vms.user_id = :user_id and ';
     		
     if($status == 2)
     	 $sql = 'select vms.*,package.*,username from vms,package,users where '.$sqlUser.' vms.package_id = package.package_id and vms.user_id = users.user_id';
     else 
     	if($status == 1)
         $sql = 'select vms.*,package.*,username from vms,package,users where '.$sqlUser.' status = 1 and vms.package_id = package.package_id and vms.user_id = users.user_id';
     else 
     	 $sql = 'select vms.*,package.*,username from vms,package,users where '.$sqlUser.' status = 0 and vms.package_id = package.package_id and vms.user_id = users.user_id';
    
     $sth = $this->_db->prepare($sql);
     
     if($user_id > 0)
     	$sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
     	
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
    }
    
    /**
     * 
     * Lay thong tin cac may ao su dung 1 goi dich vu
     * @param int $package_id
     * @return array : neu ton tai  null : neu khong ton tai
     */
    public function getVms_Package($package_id)
    {
    	$sql = 'select vms.*,username from vms,users where package_id = :package_id and vms.user_id = users.user_id';
     	$sth = $this->_db->prepare($sql);
     	$sth->bindParam(':package_id',$package_id,PDO::PARAM_INT);
     	$status = $sth->execute();
     	if (!$status)
        	return null;
     	$result = $sth->fetchAll();
     	if(count($result)>0)
        	return $result;
     	else
        	return null;  
    }
    
    /**
     * 
     * Cap nhat thoi gian gia han
     * @param unknown_type $vm_id
     * @param unknown_type $extratime
     * @return bool true: thanh cong false : that bai
     */
    public function updateExtratime($vm_id,$extratime)
    {
    	$sql = 'update vms set extratime = extratime + :extratime where vm_id = :vm_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':vm_id',$vm_id,PDO::PARAM_INT);
  	    $sth->bindParam(':extratime', $extratime, PDO::PARAM_INT);
    	return $sth->execute();
    }
    
    /**
      * Xoa mot may ao
      * @param :vm_id
      * @return bool  true: thanh cong false: that bai
      * */
      public function deleteVm($vm_id)
      {
        $sql = 'delete from vms where vm_id = :vm_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':vm_id',$vm_id,PDO::PARAM_INT);
        return $sth->execute();
      } 
    
}